<?php

namespace App\Http\Controllers;

use App\City;
use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Workshop;
use App\Park;
use App\WorkshopIndustry;
use App\WorkshopPropertyType;
use App\Area;

class WorkshopController extends Controller
{
    //工厂列表页
    public function index(Request $request)
    {
        if($request->changeCity){//城市切换
            session(['cityId' => $request->changeCity]);
            session(['cityName'=>City::findOrFail($request->changeCity)->title]);
        }
        session(['type'=>$request->type]);
        $type = session('type');//厂房类型 0出租 1出售
        $cityId=  session('cityId',City::firstOrFail()->id);
        //查询区域列表
        $area=City::where('parent_id','=',$cityId)->get();
        $area1=City::whereIn('parent_id',$area->pluck('id')->all())->get();
        //获取面积信息
        $areas = Area::all();
        $workshop= Workshop::join('park','workshop.park_id','=','park.id')
            ->where('park.city_id','=',$cityId)
            ->where('workshop.type','=',$type)
            ->select('workshop.*','park.id as parkId')
            ->get();
        //热门搜索
        $parks = Park::where('city_id','=',$cityId)->where('is_hot','=','1')->get();
        return view('houselist', ['workLists' => $workshop,'area'=>$area,'area1'=>$area1,'areas'=>$areas,'parks'=>$parks]);
    }

    //厂房详情
    public function details(Request $request)
    {
        $id = $request->input('id');
        $workshop = \App\model\Workshop::find($id);
        $park = Park::find($workshop->park_id);
        $workshopIndustry = WorkshopIndustry::find($workshop->workshop_industry_id);
        $workshopPropertyType = WorkshopPropertyType::find($workshop->workshop_property_type_id);

        return view('details', [
            'workshop' => $workshop, 
            'park' => $park, 
            'workshopIndustry'=>$workshopIndustry,
            'workshopPropertyType'=>$workshopPropertyType
        ]);
    }

    //按区域查询
    public function city(Request $request)
    {
    	$cityId = session('cityId','0');
        $area2_id = session('area2_id');
        $type = session('type',0);//厂房类型 0出租 1出售
        //关键字
        $keyword = $request->keyword;
    	//查询区域列表
        $area=City::where('parent_id','=',$cityId)->get();
        $area1=City::whereIn('parent_id',$area->pluck('id')->all())->get();
        //获取面积信息
        $areas = Area::all();
    	if($request->ids){
    		$id = $request->ids;
    		$ids = explode("-", $id);
    		$area_id = $ids[0];
    		array_shift($ids);
    		$area1_id = $ids;
    		session(['area_id' => $area_id,'area1_id'=>$area1_id]);
    		$workshop = Workshop::join('park','workshop.park_id','=','park.id')
            ->where('park.area_id','=',$area_id)
            ->where('workshop.type','=',$type)
            ->when($keyword,function($query) use($keyword){
            	return $query->where('park.title','like','%'.$keyword.'%');
            })
            ->when($area1_id,function($query) use($area1_id){
                return $query->whereIn('park.area1_id',$area1_id);
            })
            ->when($area2_id,function($query) use($area2_id){
            	return $query->whereIn('workshop.area_id',$area2_id);
            })
            ->select('workshop.*','park.id as parkId')
            ->get();
    	}else{
    		$workshop = Workshop::join('park','workshop.park_id','=','park.id')
            ->where('park.city_id','=',$cityId)
            ->where('workshop.type','=',$type)
            ->when($keyword,function($query) use($keyword){
                return $query->where('park.title','like','%'.$keyword.'%');
            })
            ->select('workshop.*','park.id as parkId')
            ->get();
    	}
        //热门搜索
        $parks = Park::where('city_id','=',session('cityId'))->where('is_hot','=','1')->get();
    	return view('houselist', ['workLists' => $workshop,'area'=>$area,'area1'=>$area1,'areas'=>$areas,'parks'=>$parks]);
    }

    //按面积查询
    public function area(Request $request)
    {
    	$cityId = session('cityId','0');
        $area_id = session('area_id');
        $area1_id = session('area1_id');
        $type = session('type',0);//厂房类型 0出租 1出售
        //关键字
        $keyword = $request->keyword;
    	//查询区域列表
        $area=City::where('parent_id','=',$cityId)->get();
        $area1=City::whereIn('parent_id',$area->pluck('id')->all())->get();
        //获取面积信息
        $areas = Area::all();
    	if($request->id){
    		$id = $request->id;
    		$area2_id = explode("-", $id);
    		session(['area2_id'=>$area2_id]);
    		$workshop = Workshop::join('park','workshop.park_id','=','park.id')
            ->where('park.city_id','=',$cityId)
            ->where('workshop.type','=',$type)
            ->whereIn('workshop.area_id',$area2_id)
            ->when($area_id,function($query) use($area_id){
            	return $query->where('park.area_id','=',$area_id);
            })
            ->when($keyword,function($query) use($keyword){
                return $query->where('park.title','like','%'.$keyword.'%');
            })
            ->when($area1_id,function($query) use($area1_id){
            	return $query->whereIn('park.area_id',$area1_id);
            })
            ->select('workshop.*','park.id as parkId')
            ->get();
    	}else{
    		$workshop = Workshop::join('park','workshop.park_id','=','park.id')
            ->where('park.city_id','=',$cityId)
            ->where('workshop.type','=',$type)
            ->when($area_id,function($query) use($area_id){
            	return $query->where('park.area_id','=',$area_id);
            })
            ->when($keyword,function($query) use($keyword){
                return $query->where('park.title','like','%'.$keyword.'%');
            })
            ->when($area1_id,function($query) use($area1_id){
            	return $query->whereIn('park.area_id',$area1_id);
            })
            ->select('workshop.*','park.id as parkId')
            ->get();
    	}
        //热门搜索
        $parks = Park::where('city_id','=',session('cityId'))->where('is_hot','=','1')->get();
    	return view('houselist', ['workLists' => $workshop,'area'=>$area,'area1'=>$area1,'areas'=>$areas,'parks'=>$parks]);
    }
}
